/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-12<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
package cn.cnsy123.bookwebsiteserver.service.dbinfo.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.cnsy123.bookwebsiteserver.entity.SysDataBaseInfo;
import cn.cnsy123.bookwebsiteserver.param.DbConfigInsertParam;
import cn.cnsy123.bookwebsiteserver.param.DbConfigUpdateParam;
import cn.cnsy123.bookwebsiteserver.repository.DbInfoDao;
import cn.cnsy123.bookwebsiteserver.service.dbinfo.DbInfoService;
import cn.cnsy123.bookwebsiteserver.util.JsonUtil;
import cn.cnsy123.bookwebsiteserver.vo.layui.CommonResult;
import cn.cnsy123.bookwebsiteserver.vo.layui.TableResult;

/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-12<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
@Service
public class DbInfoServiceImpl implements DbInfoService {

    private static final Logger logger = LoggerFactory.getLogger(DbInfoServiceImpl.class);
    @Autowired
    private DbInfoDao dbInfoDao;

    @Override
    @Transactional(readOnly = true)
    public TableResult<SysDataBaseInfo> findAllDbInfo(int page, int size) {
        List<SysDataBaseInfo> data = null;
        Page<SysDataBaseInfo> pageData = null;
        String msg = "获取数据为空,请手动配置信息";
        int flag = 1;
        long count = 0L;
        try {
            pageData = dbInfoDao.findAll(PageRequest.of(page-1, size));
            if(pageData != null) {
                data = pageData.getContent();
                count = pageData.getTotalElements();
                if(count > 0) {
                   msg = "获取数据成功";
                   flag = 0;
                }
            }
            
        } catch (Exception e) {
            logger.error("获取NOSQL数据源信息失败，原因：",e);
            msg = "获取数据出错,请通知您的系统管理员！";
            flag = 1;
            count = 0L;
            data = null;
            throw new RuntimeException("获取NOSQL数据源信息失败");
        }
        TableResult<SysDataBaseInfo> result = new TableResult<SysDataBaseInfo>(flag, msg, count, data);
        logger.info("NOSQL数据源配置中心查询结果:{}",JsonUtil.obj2jsonStr(result));
        return result;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public CommonResult<Object> deleteDbConfigInfo(Long dbInfoId) {
        CommonResult<Object> commonResult = null;
        try {
            dbInfoDao.deleteById(dbInfoId);
            commonResult = new CommonResult<>(0, "删除成功");
        } catch (Exception e) {
            logger.error("删除NOSQL数据源信息失败，原因：",e);
            throw new RuntimeException("删除失败");
        }
        return commonResult;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public CommonResult<Object> updateDbConfigInfo(DbConfigUpdateParam param) {
        CommonResult<Object> commonResult = null;
        try {
            SysDataBaseInfo sysDataBaseInfo = dbInfoDao.findByDbInfoId(param.getDbInfoId());
            sysDataBaseInfo.setDbAccessIp(param.getDbAccessIp());
            sysDataBaseInfo.setDbName(param.getDbName());
            sysDataBaseInfo.setDbType(param.getDbType());
            sysDataBaseInfo.setPassWord(param.getPassWord());
            sysDataBaseInfo.setPort(param.getPort());
            dbInfoDao.save(sysDataBaseInfo);
            commonResult = new CommonResult<>(0, "更新成功");
        } catch (Exception e) {
            logger.error("更新NOSQL数据源信息失败，原因：",e);
            throw new RuntimeException("更新失败");
        }
        return commonResult;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public CommonResult<Object> insertDbConfigInfo(DbConfigInsertParam param) {
        CommonResult<Object> commonResult = null;
        try {
            SysDataBaseInfo sysDataBaseInfo = new SysDataBaseInfo();
            sysDataBaseInfo.setDbAccessIp(param.getDbAccessIp());
            sysDataBaseInfo.setDbName(param.getDbName());
            sysDataBaseInfo.setDbType(param.getDbType());
            sysDataBaseInfo.setPassWord(param.getPassWord());
            sysDataBaseInfo.setPort(param.getPort());
            dbInfoDao.save(sysDataBaseInfo);
            commonResult = new CommonResult<>(0, "保存成功");
        } catch (Exception e) {
            logger.error("保存NOSQL数据源信息失败，原因：",e);
            throw new RuntimeException("保存失败");
        }
        return commonResult;
    }

}
